package com.sks.web.action.news;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.PageView;
import com.sks.bean.QueryResult;
import com.sks.bean.pojo.News;

import com.sks.service.news.NewsService;
import com.sks.utils.StringUtil;
import com.sks.web.action.BasicAction;
import com.sks.web.formbean.news.NewsForm;

/**
 * 后台新闻列表
 * @author yuanjiayuan
 *
 */
@Controller("/cms/news/list")
public class NewsListAction extends BasicAction{
	@Resource(name="newsServiceBean")
	private NewsService newsService;


	/*------------------后台新闻列表展示---------------------*/
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		NewsForm formbean = (NewsForm) form;		
		StringBuffer jpql = new StringBuffer("1=1");
		List<Object> params = new ArrayList<Object>();
		if ("true".equals(formbean.getQuery())) {
			if (!StringUtil.isEmpty(formbean.getTitle())) {
				params.add("%" + formbean.getTitle() + "%");
				jpql.append(" and o.title like ?" + params.size());
			}
			if (!StringUtil.isEmpty(formbean.getStatus())) {
				params.add(formbean.getStatus());
				jpql.append(" and o.status = ?" + params.size());
			}
		}
		PageView<News> pageView = new PageView<News>(50, formbean.getPage());
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("newsId", "desc");
		pageView.setQueryResult(newsService.getScrollData(pageView
				.getFirstResult(), pageView.getMaxresult(), jpql.toString(),
				params.toArray(), orderby));
		request.setAttribute("pageView", pageView);
		this.setReadOnly(request, "list");
		return mapping.findForward(LIST);	
	}
	
	

	
	
}
